package com.xgp.labblogaffter.Controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 登陆相关
 */
@RestController()
public class loginController {

    @Value("${login.root.username}")
    String rootUsername;

    @Value("${login.root.password}")
    String rootPassword;

    /**
     * 该方法接受登陆名和密码，并进行判断，放回是否能够登陆
     */
    @PostMapping("/eqLogin")
    public boolean eqLogin(String username, String password, HttpSession session) {
        try {
            //        System.out.println(username + "===" + password);
//        System.out.println(rootUsername + "===" + rootPassword);
            if (rootUsername.equals(username) && rootPassword.equals(password)) {

                if (session.getAttribute("login") != null) {
                    return false;
                }
                //往session中存值
                session.setAttribute("login", "ok");

                return true;
            }
            return false;
        }catch (Exception e) {
            return false;
        }
    }

    /**
     * 清楚登陆状态的方法
     * @param session
     * @return
     */
    @PostMapping("/logout")
    public boolean logout(HttpSession session) {
        try {
            String loginOk = (String) session.getAttribute("login");
            if (loginOk == null) {
                return false;
            }
            if ("ok".equals(loginOk)) {
                session.removeAttribute("login");
                return true;
            }
            return false;
        }catch (Exception e) {
            return false;
        }

    }

}
